<?php
namespace app\admin\controller\set;
use \app\admin\controller\AuthSite;
class News extends AuthSite{
    
    
    /**
     * 列表
     */
    public function index(){
        try{
            if(request()->isPost() ){
                $where = [
                    'status' => input('status'),
                    'keyword'=> input('keyword',''),
                    'groupid'=> input('groupid',0),
                    'page'   => input('page'),
                ];
                $NewsManage = new \logic\set\news\NewsManage($this->getUser());
                $list = $NewsManage->getList($where);
                return $this->success('成功','',$list);
            }
            $NewsGroupManage = new \logic\set\news\NewsGroupManage();
            $grouplist = $NewsGroupManage->getList(['siteid'=>$this->siteId,'status'=> 1]);
            
            $this->assign(get_defined_vars());
            return $this->fetch();
        }catch( \think\exception $e){
            $this->error($e->getMessage());
        }
    }
    
    
    /**
     * 添加
     */
    public function add(){  
        $site = $this->getSite()->getInfo();
        try{
            if( request()->isPost() ){   
                $data = [            
                    'siteid'    => $this->siteId,
                    'adduserid' => $this->userId,       
                    'groupid'   => input('groupid',0),
                    'tags'      => input('tags'),  
                    'sort'      => input('sort'),
                    'pic'       => input('pic'),
                    'name'      => input('name'),
                    'author'    => input('author'),
                    'des'       => input('des'),
                    'detail'    => input('detail'),
                    'seo'       => input('seo'),           
                ];
                $NewsManage = new \logic\set\news\NewsManage($this->getUser());
                $id = $NewsManage->add($data);
                return $this->success('添加成功','/set/news/index');
            }       
            $NewsGroupManage = new \logic\set\news\NewsGroupManage();
            $grouplist = $NewsGroupManage->getList(['siteid'=>$this->siteId,'status'=> 1]);
            $grouplist = \core\helper\Tools::arrayMerge($grouplist); 
            $this->assign(get_defined_vars());
            return $this->fetch();
            
        }catch( \think\Exception $e){
            $this->error($e->getMessage());
        }
    }
    
    /**
     * 修改
     */
    public function edit(){        
        
        $id = input('id');
        if( request()->isPost() ){   
            $data = [            
                'adduserid' => $this->userId,       
                'groupid'   => input('groupid',0),
                'tags'      => input('tags'),  
                'sort'      => input('sort'),
                'pic'       => input('pic'),
                'name'      => input('name'),
                'author'    => input('author'),
                'des'       => input('des'),
                'detail'    => input('detail'),
                'seo'       => input('seo'),           
            ];
            $NewsManage = new \logic\set\news\NewsManage($this->getUser());
            $id = $NewsManage->edit($id,$data);
            return $this->success('修改成功','/set/news/index');
        }
        $NewsManage = new \logic\set\news\NewsManage($this->getUser());
        $info = $NewsManage->getInfo(['id'=>$id]);
        $NewsGroupManage = new \logic\set\news\NewsGroupManage();
        $grouplist = $NewsGroupManage->getList(['siteid'=>$this->siteId,'status'=> 1]);
        $grouplist = \core\helper\Tools::arrayMerge($grouplist);
        $tags = array_filter(explode(',',$info['tags']));
        $seo  = json_decode($info['seo'],true);
        $this->assign(get_defined_vars());
        return $this->fetch();
    }
    
    /**
     * 更新状态
     */
    public function editstatus(){
        try{
            $id = input('id');
            $NewsManage = new \logic\set\news\NewsManage($this->getUser());
            $NewsManage->editStatus($id);
            $this->success('成功');
        }catch( \think\Exception $e){
            $this->error($e->getMessage());
        }
    }
    
    /**
     * 更新状态
     */
    public function del(){
        try{
            $id = input('id');
            $this->success('成功');
        }catch( \think\Exception $e){
            $this->error($e->getMessage());
        }
    }
    
}